from django.db import models

from lapi_app.models.user_model.user_info import Userinfo


class Package(models.Model):
    package_name = models.CharField(max_length=255, verbose_name='包名称', null=True)
    package = models.CharField(max_length=255, verbose_name='包名', null=True)
    type = models.IntegerField(default=1, verbose_name='类型', null=False)  # 1. Android， 2：IOS

    def __str__(self):
        return self.package


class App_menu(models.Model):
    label = models.CharField(max_length=38, verbose_name='节点名称')
    pid = models.IntegerField(default=1, verbose_name='父id')
    type = models.IntegerField(default=1, verbose_name='类型', null=False)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    create_time = models.DateTimeField("创建时间", auto_now_add=True, null=True)
    update_time = models.DateTimeField("编辑时间", auto_now_add=True, null=True)

    def __str__(self):
        return self.label


class App_img(models.Model):
    img_name = models.CharField(max_length=38, verbose_name='图像名称')
    img_address = models.CharField(max_length=255, verbose_name='图像地址')
    img_view = models.CharField(max_length=255, verbose_name='图像')
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.img_name


class App_script(models.Model):
    menu = models.ForeignKey(App_menu, on_delete=models.CASCADE)
    label = models.CharField(max_length=38, verbose_name='步骤名称')
    step = models.IntegerField(default=1, verbose_name='执行顺序', null=False)
    type = models.IntegerField(default=1, verbose_name='类型', null=False)
    img = models.ForeignKey(App_img, on_delete=models.CASCADE, null=True)
    # 1: 启动app， 2：点击， 3：输入, 4：上下左右滑动
    img_address = models.JSONField(default=dict, verbose_name='图像地址', null=True)
    # 区分不同操作系统地址
    swipe = models.JSONField(default=dict, null=True, verbose_name="上下左右滑动")
    element_type = models.IntegerField(default=1, verbose_name='元素定位类型', null=True)  # 1: xpath，2：css，3：id，4：text
    address = models.TextField(null=True, verbose_name='元素地址', default="")
    input = models.TextField(null=True, verbose_name='输入值', default="")
    package = models.JSONField(default=dict, null=True, verbose_name='包名')
    wait_time = models.IntegerField(null=True, verbose_name='等待时间', default=0)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.label


class App_result(models.Model):
    result_id = models.TextField(null=False, verbose_name='结果id', default="")
    device = models.TextField(default="", null=True, verbose_name="设备名称")
    result_type = models.IntegerField(null=False, verbose_name='结果类型', default=1)  # 1：执行通过，0：执行失败
    result = models.JSONField(default=dict, null=True, verbose_name="执行结果")
    result_video = models.TextField(default="mp4", null=True, verbose_name="MP4文件地址")
    result_img = models.TextField(default="", null=True, verbose_name="图片文件地址")
    cpu = models.JSONField(default=list, null=True, verbose_name="cpu")
    memory = models.JSONField(default=list, null=True, verbose_name="内存")
    up_network = models.JSONField(default=list, null=True, verbose_name="上传速度")
    down_network = models.JSONField(default=list, null=True, verbose_name="下载速度")
    disk = models.JSONField(default=dict, null=True, verbose_name="磁盘IO")
    temperature = models.JSONField(default=list, null=True, verbose_name="温度")
    time = models.JSONField(default=list, null=True, verbose_name="时间")
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    menu = models.ForeignKey(App_menu, on_delete=models.CASCADE, null=True)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.result


class App_result_list(models.Model):
    result_id = models.TextField(null=False, verbose_name='结果id', default="")
    devices = models.JSONField(default=dict, null=True, verbose_name="设备列表")
    menu = models.ForeignKey(App_menu, on_delete=models.CASCADE, null=True)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    def __str__(self):
        return self.result_id


class App_run_list(models.Model):
    task_name = models.TextField(null=False, verbose_name='任务名称', default="")
    result_id = models.TextField(null=False, verbose_name='结果id', default="")
    scripts = models.JSONField(default=list, null=True, verbose_name="步骤列表")
    devices = models.JSONField(default=list, null=True, verbose_name="设备列表")
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    update_time = models.DateTimeField("更新时间", auto_now_add=True)

    def __str__(self):
        return self.task_name
